package com.shbaiche.bifenba.ui.my;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.shbaiche.bifenba.App;
import com.shbaiche.bifenba.R;
import com.shbaiche.bifenba.model.base.TransMessage;
import com.shbaiche.bifenba.model.user.FavariteMessage;
import com.shbaiche.bifenba.model.video.ArticleGroupVo;
import com.shbaiche.bifenba.model.video.ArticleVo;
import com.shbaiche.bifenba.model.video.AuthorMessage;
import com.shbaiche.bifenba.net.JsonCallback;
import com.shbaiche.bifenba.net.NetApi;
import com.shbaiche.bifenba.ui.base.BaseActivity;
import com.shbaiche.bifenba.ui.home.CategoryTabStrip;
import com.shbaiche.bifenba.ui.home.sub.SubActivity;
import com.shbaiche.bifenba.ui.home.sub.SubInfoFragment;
import com.shbaiche.bifenba.util.StringUtil;
import com.shbaiche.bifenba.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.droidlover.xdroidbase.base.XFragmentAdapter;
import cn.droidlover.xdroidbase.router.Router;
import okhttp3.Call;


/**
 * 我的收藏
 */

public class MyCollectionActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar toolbar;

    @BindView(R.id.category_strip)
    CategoryTabStrip tabs;

    @BindView(R.id.view_pager)
    ViewPager pager;

    Dialog dg;

    List<Fragment> fragmentList = new ArrayList<>();
    XFragmentAdapter adapter;

    private List<ArticleVo> articleList;

    private List<ArticleVo> videoList;

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void initData(Bundle savedInstanceState) {

        if (App.getPassport() == null) {
            ToastUtil.show("请先登录!");
            finish();
            return;
        }

        dg = new Dialog(this);
        dg.setContentView(R.layout.view_loading);
        dg.show();


        FavariteMessage msg = new FavariteMessage();
        msg.setPassport(App.getPassport());
        msg.setPageSize(50);
        msg.setPageIndex(0);
        msg.setType(ArticleGroupVo.Type.ARTICLE);
        NetApi.MyFavorite(msg, new JsonCallback<TransMessage>() {

            @Override
            public void onFail(Call call, Exception e, int id) {
                dg.hide();
            }

            @Override
            public void onError(Call call, Exception e, int id) {
                ToastUtil.show("onFail" + e.getMessage());
                dg.hide();
            }

            @Override
            public void onResponse(TransMessage response, int id) {
                Gson gson = new Gson();
                FavariteMessage msg = gson.fromJson(response.getMessage(), FavariteMessage.class);

                if (StringUtil.equalsIgnoreCase(TransMessage.SuccessCode, msg.getCode())) {
                    articleList = msg.getVoList();
                    load();
                } else {
                }
            }
        });
    }

    private void load(){
        FavariteMessage msg = new FavariteMessage();
        msg.setPassport(App.getPassport());
        msg.setPageSize(50);
        msg.setPageIndex(0);
        msg.setType(ArticleGroupVo.Type.VIDEO);
        NetApi.MyFavorite(msg, new JsonCallback<TransMessage>() {

            @Override
            public void onFail(Call call, Exception e, int id) {
                dg.hide();
            }

            @Override
            public void onError(Call call, Exception e, int id) {
                ToastUtil.show("onFail" + e.getMessage());
                dg.hide();
            }

            @Override
            public void onResponse(TransMessage response, int id) {
                dg.hide();
                Gson gson = new Gson();
                FavariteMessage msg = gson.fromJson(response.getMessage(), FavariteMessage.class);

                if (StringUtil.equalsIgnoreCase(TransMessage.SuccessCode, msg.getCode())) {
                    videoList = msg.getVoList();
//                    fragmentList.clear();
//                    fragmentList.add(new SubInfoFragment(msg.getArticleList(), 1));
//                    fragmentList.add(new SubInfoFragment(msg.getVedioList(), 2));
//
//                    if (adapter == null) {
//                        adapter = new XFragmentAdapter(getSupportFragmentManager(), fragmentList, titles);
//                    }

                    fragmentList.clear();
                    fragmentList.add(new SubInfoFragment(articleList, 1));
                    fragmentList.add(new SubInfoFragment(videoList, 2));

                    if (adapter == null) {
                        adapter = new XFragmentAdapter(getSupportFragmentManager(), fragmentList, new String[]{"资讯", "视频"});
                    }

                    pager.setAdapter(adapter);

                    tabs.setViewPager(pager);
                    tabs.setAdaptive(true);
                } else {
                }
            }
        });
    }

    @Override
    public void setListener() {

    }


    public static void launch(Activity activity) {
        Router.newIntent()
                .from(activity)
                .to(MyCollectionActivity.class)
                .data(new Bundle())
                .launch();
    }

    @Override
    protected void onDestroy() {
        if (dg != null) {
            dg.dismiss();
            dg = null;
        }
        super.onDestroy();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_follow;
    }

    @Override
    public void setToolbar() {
        initToolbar(toolbar, "我的收藏");
    }
}
